package com.gitee.l0km.codegen.decorator;

import java.util.List;
import java.util.Set;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;

import com.gitee.l0km.codegen.base.generator.GeneratorConfiguration;
import com.gitee.l0km.com4j.base.ClassLoaderUtils;
import com.gitee.l0km.javadocreader.JavadocReader;
import com.google.common.collect.Sets;

import static com.gitee.l0km.com4j.base.MiscellaneousUtils.elementsOf;
import static com.google.common.base.Preconditions.checkArgument;


/**
 * spring decorator生成器参数对象
 * @author guyadong
 *
 */
public class SpringDecoratorConfiguration extends GeneratorConfiguration implements SpringDecoratorConstants{
	public static final String SPRING_FOLDER = "spring";
	/**
	 *  implement Class
	 */
	private Class<?> implClass;
	private String sourcepath;
	private String classpath;
	
	/**
	 * exclude annotation Class names which will be skip for generation
	 */
	private Set<String> excludeAnnotations;
	/**
	 * use Response family class in common-java(com.gitee.l0km:common-base:2.2.0 or above) in spring.controller.class.vm,
	 * if false, generate Response family class in Spring Controller class source 
	 */
	private boolean useCommonResponse = true;
	
	/**
	 * prefix of path for service port,default is implement class name
	 */
	private String portPrefix;
	private boolean timecost;
	private boolean withDelegateMethod;
	public SpringDecoratorConfiguration() {
		super();
		// 指定模板文件夹
		this.defaultValue.setProperty(TEMPLATE_FOLDER_OPTION_LONG, SPRING_FOLDER);
		// 指定sourcepath的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(SOURCE_PREFIX_OPTION_LONG,"");
		// 指定classpath的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(CLASS_PATH_OPTION_LONG,"");
		// 指定useCommonResponse的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(USE_COMMON_RESPONSE_OPTION_LONG, true);
		// 指定excludeAnnotations的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(EXCLUDE_ANN_OPTION_LONG,"");
		// 指定portPrefix的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(PORT_PREFIX_OPTION_LONG,"");
		// 指定timecost的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(TIMECOST_OPTION_LONG,false);
		// 指定beanfilterDelegate的默认值，避免默认值为{@code null}
		this.defaultValue.setProperty(WITH_DELEGATE_METHOD_OPTION_LONG,true);
	}
	
	@Override
	public void loadConfig(Options options, CommandLine cmd) throws ParseException {
		super.loadConfig(options, cmd);
		sourcepath = getProperty(SOURCE_PREFIX_OPTION_LONG);
		classpath = ClassLoaderUtils.normalizeClasspath((String)getProperty(CLASS_PATH_OPTION_LONG));
		JavadocReader.setSourcepath(sourcepath);
		JavadocReader.setClasspath(classpath);
		JavadocReader.resetRoot();
		List<Class<?>> implClasses = getClasses(IMPL_CLASS_OPTION_LONG, classpath, false);
		checkArgument(!implClasses.isEmpty(),"NOT implement class defiend");
		implClass = implClasses.get(0);
		useCommonResponse = getProperty(USE_COMMON_RESPONSE_OPTION_LONG);
		excludeAnnotations = Sets.newHashSet(elementsOf(getProperty(EXCLUDE_ANN_OPTION_LONG)));
		portPrefix = getProperty(PORT_PREFIX_OPTION_LONG);
		timecost = getProperty(TIMECOST_OPTION_LONG);
		withDelegateMethod = getProperty(WITH_DELEGATE_METHOD_OPTION_LONG);
	}

	/**
	 * @return refClass
	 */
	public Class<?> getImplClass() {
		return implClass;
	}

	/**
	 * @return sourcepath
	 */
	public String getSourcepath() {
		return sourcepath;
	}

	/**
	 * @return classpath
	 */
	public String getClasspath() {
		return classpath;
	}

	/**
	 * @return useCommonResponse
	 */
	public boolean isUseCommonResponse() {
		return useCommonResponse;
	}

	public Set<String> getExcludeAnnotations() {
		return excludeAnnotations;
	}

	public String getPortPrefix() {
		return portPrefix;
	}

	/**
	 * @since 2.3.0
	 */
	public boolean isTimecost() {
		return timecost;
	}
	/**
	 * @since 2.3.2
	 */
	public boolean isWithDelegateMethod() {
		return withDelegateMethod;
	}
}
